// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

import './common/common.css'

import globalJs from './common/common.js'
import axios from 'axios'
// vuex
import store from './store'

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import ajax from './util/ajax'
import 'jquery'

Vue.use(globalJs)

Vue.prototype.$http = axios

Vue.use(ElementUI);

Vue.config.productionTip = false

// 修改页面title
const changeTitle = (to) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
};
const filter = (to, next) => {
  if (!to.meta.filter) {
    next();
  }

  Vue.prototype.$$get('/security/login')
    .then(res => {
      window.canLogin = false;
      if (res.data.code !== 0 || res.data.data !== 0) {
        next('/login');
        document.title = '少侠，请先登录哈~';
      } else {
        next();
      }
    })
    .catch(e => {
      console.error(e);
      next('/login');
    })
}

router.beforeEach((to, from, next) => {
  changeTitle(to);
  filter(to, next);
});
// filter
Vue.use(ajax);

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store, // 使用store
  components: { App },
  template: '<App/>'
})
